<template>
  <el-row style="line-height: 60px">
    <el-col :span="20" style="display: inline-flex">
      <span :title="isCollapsed ? '展开' : '收起'" style="line-height: 60px;margin-right: 2rem;"
            :class=" isCollapsed?'el-icon-s-unfold cursor-pointer':'el-icon-s-fold cursor-pointer'"
            @click="setCollapse"></span>
      <span v-if="breadData!=null">
              <el-breadcrumb separator-class="el-icon-arrow-right" style="height: 60px;line-height: 60px;"
                             v-if="breadData.child===null">
        <el-breadcrumb-item :to="{path:breadData.name}">{{ breadData.parent }}</el-breadcrumb-item>
      </el-breadcrumb>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="height: 60px;line-height: 60px;" v-else>
        <el-breadcrumb-item><span v-text="breadData.parent"></span></el-breadcrumb-item>
        <el-breadcrumb-item><span v-text="breadData.child"></span></el-breadcrumb-item>
      </el-breadcrumb>
      </span>

    </el-col>
    <el-col :span="4" v-if="userInfo" style="text-align: right">
      <el-avatar :src="userInfo.avatar" style="line-height: 60px!important;" v-if="userInfo.avatar">

      </el-avatar>
      <el-avatar style="background-color: #409eff" v-else>
        <span v-text="userInfo.name"></span>
      </el-avatar>
      <el-dropdown>
        <span class="el-dropdown-link">
          <span v-text="userInfo.name"></span> <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item><span @click="dialogFormVisible = true">个人资料</span>
          </el-dropdown-item>
          <el-dropdown-item><span @click="$router.push('/login')">注销登录</span></el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-col>
    <el-dialog title="个人资料" :visible.sync="dialogFormVisible" width="200">
      <el-form :model="userInfo" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<!--        <el-divider content-position="left">通讯信息修改</el-divider>-->
        <el-form-item label="手机号码" prop="telephone">
          <el-input v-model="userInfo.telephone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userInfo.email"></el-input>
        </el-form-item>
<!--        <el-divider content-position="left">密码修改</el-divider>-->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="userInfo.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="configPassword">
          <el-input type="password" v-model="userInfo.configPassword"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消
        </el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>

      </div>
    </el-dialog>
  </el-row>
</template>
<script>
export default {
  name: "HeaderComponent",
  data() {
    // 验证手机号的正则表达式
    const checkPhone = (rule, value, callback) => {
      const reg = /^1[3-9]\d{9}$/;
      if (value === '') {
        callback(new Error('手机号不能为空'));
      } else if (!reg.test(value)) {
        callback(new Error('请输入正确的手机号'));
      } else {
        callback();
      }
    };

    return {
      isCollapsed: false,
      userInfo: {
        id: '',
        telephone: '',
        email: '',
        password: '',
        configPassword: ''
      },
      firstName: null,
      secondName: null,
      breadData: {},
      dialogFormVisible: false,
      rules: {
        telephone: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {validator: checkPhone, trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        configPassword: [
          {required: true, message: '请再次输入密码', trigger: 'blur'},
          {
            validator: (rule, value, callback) => {
              debugger;
              if (value !== this.userInfo.password) {
                callback(new Error('两次输入的密码不一致，请重新输入'));
              } else {
                callback();
              }

            }, trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.isCollapsed = this.$store.state.isCollapsed
    this.userInfo = this.$store.state.userInfo
    this.userInfo.password = '';
    this.$set(this.userInfo, 'configPassword', '');
    this.breadData = this.$store.state.breadData
  },
  watch: {
    "$route.path": {
      handler(newV, oldV) {
        console.log(newV, oldV)
        console.log(this.$route.path)
        this.$store.state.allBreadData.find(item => {
          if (item.name === newV) {
            this.breadData = item
            this.$store.commit("setBread", item)
          }
        })
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    closeDialog() {
      this.dialogFormVisible = false;
      this.userInfo.password = '';
      this.userInfo.configPassword = '';
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post("/user/update/password", this.userInfo).then(res => {
            if (res.data.success) {
              this.$tools.showMessage("修改成功，请重新登录！", "success")
              this.$router.push("/login")
            } else {
              this.$tools.showMessage("修改失败", "error")
            }
          })
        }
      });
    },
    setCollapse() {
      this.isCollapsed = !this.isCollapsed
      this.$store.commit("setCollapsed", this.isCollapsed)
      console.log("Collapsed", this.isCollapsed)
    },
    edit() {
      this.dialogFormVisible = true;
    }
  }
}
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}

.el-icon-arrow-down {
  font-size: 12px;
}
</style>
